package com.atguigu.springcloud.alibaba.service.impl;

import com.atguigu.springcloud.alibaba.dao.OrderDao;
import com.atguigu.springcloud.alibaba.domain.Order;
import com.atguigu.springcloud.alibaba.service.AccoutService;
import com.atguigu.springcloud.alibaba.service.OrderService;
import com.atguigu.springcloud.alibaba.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: mdh
 * @Description:
 * @Date: Created in 11:58 2021/3/9
 * @Modified By:
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private AccoutService accoutService;
    @Resource
    private StorageService storageService;

    @Override
    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public void create(Order order) {
        try {
            log.info("------>开始创建订单");
            orderDao.create(order);

            log.info("----> 订单微服务开始调用库存，做扣减count");
            storageService.decrease(order.getProductId(),order.getCount());
            log.info("----> 订单微服务开始调用库存，做扣减end");

            log.info("----->订单微服务开始调用账户微服务，做扣减money");
            accoutService.decrease(order.getUserId(),order.getMoney());
            log.info("----->订单微服务开始调用账户微服务，做扣减end");

            log.info("------>修改订单状态开始");
            orderDao.update(order.getUserId(),1);
            log.info("------>修改订单状态结束");

            log.info("------>下订单结束，O(∩_∩)O哈哈~");
        } catch (Exception e) {
            log.error("系统出现错误了┭┮﹏┭┮："+e);
        }
    }
}
